package com.sonicvision.resourcetracker.mailmerge;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class MailMergeMetadata {

	private String email;
	private String password;
	private String subject;
	private String body;

	public static MailMergeMetadata of(InputStream inputStream)
			throws IOException {

		// Get Details sheet from the workbook
		XSSFSheet sheet = new XSSFWorkbook(inputStream).getSheet("Details");

		String email = null;
		String password = null;
		String subject = null;
		String body = null;

		// Iterate through each rows from first sheet
		Iterator<Row> rowIterator = sheet.iterator();
		while (rowIterator.hasNext()) {
			Row row = rowIterator.next();

			String key = row.getCell(0).getStringCellValue();
			String value = row.getCell(1).getStringCellValue();

			switch (key) {
			case "Email":
				email = value;
				break;
			case "Password":
				password = value;
				break;
			case "Subject":
				subject = value;
				break;
			case "Body":
				body = value;
				break;
			default:
				break;
			}
		}

		return new MailMergeMetadata(email, password, subject, body);
	}

	private MailMergeMetadata(String email, String password, String subject,
			String body) {
		super();
		this.email = email;
		this.password = password;
		this.subject = subject;
		this.body = body;
	}

	public String getEmail() {
		return email;
	}

	public String getPassword() {
		return password;
	}

	public String getSubject() {
		return subject;
	}

	public String getBody() {
		return body;
	}

}
